package com.example.shopingcenter.DateBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.shopingcenter.enity.CartInfo;
import com.example.shopingcenter.enity.GoodsInfo;

import java.util.ArrayList;
import java.util.List;

public class ShoppingDbHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "shopping.db";

    private static final String TABLE_GOODS_INFO = "goods_info";
    private static final String TABLE_CART_INFO = "cart_info";

    private static final int DB_VERSION = 2;

    private static ShoppingDbHelper dbHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;


    public ShoppingDbHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static ShoppingDbHelper getDbHelper(Context context) {
        if (dbHelper==null){
            dbHelper=new ShoppingDbHelper(context);
        }
        return dbHelper;
    }


    public SQLiteDatabase openReadLink() {
        if (mRDB == null || !mRDB.isOpen()) {
            mRDB = dbHelper.getReadableDatabase();
        }
        return mRDB;
    }

    public SQLiteDatabase openWriteLink() {
        if (mWDB == null || !mWDB.isOpen()) {
            mWDB = dbHelper.getWritableDatabase();
        }
        return mWDB;
    }

    public void closeLink() {
        if (mRDB != null && mRDB.isOpen()) {
            mRDB.close();
            mRDB = null;
        }
        if (mWDB != null && mWDB.isOpen()) {
            mWDB.close();
            mWDB = null;
        }
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_GOODS_INFO +
                "("
                +"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + "name VARCHAR NOT NULL,"
                + "description VARCHAR NOT NULL,"
                + " price FLOAT NOT NULL,"
                + " pic_path VARCHAR NOT NULL"
                +");";



        db.execSQL(sql);

        sql = "CREATE TABLE IF NOT EXISTS  " + TABLE_CART_INFO +
                "("
                +"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + " goods_id INTEGER NOT NULL,"
                + " count INTEGER NOT NULL"+
                ");";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void insertGoodsInfos(List<GoodsInfo> list) {
        try {
            mWDB.beginTransaction();
            for (GoodsInfo info : list) {
                ContentValues values = new ContentValues();
                values.put("name", info.name);
                values.put("description", info.description);
                values.put("price", info.price);
                values.put("pic_path", info.picPath);
                mWDB.insert(TABLE_GOODS_INFO, null, values);

            }
            mWDB.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mWDB.endTransaction();
        }

    }


    public List<GoodsInfo> queryAllGoodsInfo() {

        String sql = "select * from " + TABLE_GOODS_INFO;
        List<GoodsInfo> list = new ArrayList<>();
        Cursor cursor = mRDB.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            GoodsInfo info = new GoodsInfo();
            info.id = cursor.getInt(0);
            info.name = cursor.getString(1);
            info.description = cursor.getString(2);
            info.price = cursor.getFloat(3);
            info.picPath = cursor.getString(4);
            list.add(info);

        }
        cursor.close();
        return list;
    }

    public void insertCartInfo(int goodsId) {
        CartInfo cartInfo= queryCartInfoByGoodsId(goodsId);
        ContentValues values=new ContentValues();
        values.put("goods_id",goodsId);
        if (cartInfo==null){
            values.put("count",1);
            mWDB.insert(TABLE_CART_INFO,null,values);

        }else {
            values.put("_id",cartInfo.id);
            values.put("count",++cartInfo.count);
            mWDB.update(TABLE_CART_INFO,values,"_id=?",new String[]{String.valueOf(cartInfo.id)});

        }
    }

    private CartInfo queryCartInfoByGoodsId(int goodsId) {
Cursor cursor=mRDB.query(TABLE_CART_INFO,null,"goods_id=?",new String[]{String.valueOf(goodsId)},null,null,null);
      CartInfo info=null;
      if (cursor.moveToNext()){
          info=new CartInfo();
          info.id=cursor.getInt(0);
          info.goodsId=cursor.getInt(1);
          info.count=cursor.getInt(2);

      }

        return info;
    }

    public int countCartInfo() {
        int count=0;
        String sql="select sum(count) from "+TABLE_CART_INFO;
        Cursor cursor=mRDB.rawQuery(sql,null);
        if (cursor.moveToNext()){
            count=cursor.getInt(0);

        }
        return count;
    }

    public List<CartInfo> queryAllCartInfo() {
         List<CartInfo>list=new ArrayList<>();
         Cursor cursor=mRDB.query(TABLE_CART_INFO,null,null,null,null,null,null);
         while (cursor.moveToNext()){
             CartInfo info=new CartInfo();
             info.id=cursor.getInt(0);
             info.goodsId=cursor.getInt(1);
             info.count=cursor.getInt(2);
             list.add(info);

         }
         return list;
    }

    public GoodsInfo queryGoodsInfoById(int goodsId) {
             GoodsInfo info=null;
             Cursor cursor=mRDB.query(TABLE_GOODS_INFO,null,"_id=?",new String[]{String.valueOf(goodsId)},null,null,null);
             if (cursor.moveToNext()){
                 info=new GoodsInfo();
                 info.id = cursor.getInt(0);
                 info.name = cursor.getString(1);
                 info.description = cursor.getString(2);
                 info.price = cursor.getFloat(3);
                 info.picPath = cursor.getString(4);


             }
             return info;
    }

    public void deleteCartInfoByGoodsId(int goodsId) {
        mWDB.delete(TABLE_CART_INFO,"goods_id=?",new String[]{String.valueOf(goodsId)});

    }
    public void deleteAllCartInfo(){
        mWDB.delete(TABLE_CART_INFO,"1=1",null);
    }
}
